package com.demo.controller;

import com.alibaba.fastjson.JSON;
import com.demo.model.test2.Address;
import com.demo.mapper.test2.AddressMapper;
import com.example.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.Executors;

@RestController
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private PersonService personService;

    @GetMapping("getAddress")
    public String getAddress(Integer id){
        Address address = addressMapper.selectById(id);
        return JSON.toJSONString(address);
    }

    @PostMapping("saveAddress")
    public String saveAddress(Integer id){
        Boolean execute = transactionTemplate.execute(transactionStatus -> {
            int insert = addressMapper.insert();
            int update = addressMapper.update(id);
            return true;
        });
        System.out.println(execute);
        return "success";

    }

    @GetMapping("addRedis")
    public String addRedis(){
        Long stock = stringRedisTemplate.opsForValue().increment("stock",1);
        System.out.println(stock);
        ThreadLocal<Object> objectThreadLocal = new ThreadLocal<>();
        return stock.toString();
    }

    @GetMapping("reduceRedis")
    public String reduceRedis(){
        Long stock = stringRedisTemplate.opsForValue().decrement("stock",1);
        System.out.println(stock);
        Executors.newCachedThreadPool();
        Executors.newScheduledThreadPool(10);
        Executors.newFixedThreadPool(10);
        Executors.newSingleThreadExecutor();

        return stock.toString();
    }

    @GetMapping("/testStarter")
    public String testStarter(){
        personService.sayHello();
        return "success";
    }
}
